<template>
    <ZWDialog
        :title="(state.title as string)"
        :width="(state.width as string)"
        :show="isShowDialog"
        :show-foot="false"
        @close="closeDialog"
        @cancel="cancelDialog"
        @confirm="confirmDialog">
        <div class="content">
            <div class="module setting">
                <div class="head">
                    <div class="title">活动链接设置</div>
                </div>
                <div class="details details_setting">
                    <ImageUpload v-model="form.img_src" @change="imageUploadChange" />
                    <div class="input_text">
                        <div class="input_item">
                            <div class="input_title">标题</div>
                            <el-input
                                v-model="form.title"
                                maxlength="20"
                                show-word-limit
                                :placeholder="(state.titlePlaceholder as string)"
                                clearable
                                @blur="inputChange" />
                        </div>
                        <div class="input_item">
                            <div class="input_title">描述</div>
                            <el-input
                                v-model="form.description"
                                :autosize="{ minRows: 2, maxRows: 2 }"
                                maxlength="30"
                                type="textarea"
                                :rows="2"
                                :placeholder="(state.descriptionPlaceholder as string)"
                                show-word-limit
                                clearable
                                @blur="inputChange" />
                        </div>
                    </div>
                </div>
                <div class="tips">{{ state.tips }}</div>
            </div>
            <div class="module share">
                <div class="head share-title">
                    <div class="title">活动分享</div>
                </div>
                <div class="details details_share">
                    <ShareTypeConfiguration
                        :un-select-list="unSelectList"
                        :account-type="(state.type as number)"
                        :material-type="(props.type as number)"
                        :media_content_id="media_content_id"
                        @configure="isShowDialog = false" />
                </div>
            </div>
        </div>
    </ZWDialog>
</template>

<script setup lang="ts">
import { reactive, ref, computed, watch, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import ZWDialog from '@/components/zw/dialog/Dialog';
import ImageUpload from './ImageUpload.vue';
import ShareTypeConfiguration from './ShareTypeConfiguration.vue';

import { getLink, setLink } from '@/api/smartMarketing/drainage/groupFission';
import { endLoading, startLoading } from '@/utils/request';

interface StateType {
    [propsName: string]: string | number;
}

const unSelectList = ref<Array<number>>([7, 8]); // 不需要选择账号类型的列表
const state = reactive<StateType>({
    title: '推广',
    width: '800',
    titlePlaceholder: '客易达内容',
    descriptionPlaceholder: '这是一份有趣的内容，快来看看吧！',
    tips: '支持JPG、PNG格式，大小200KB以内，建议长宽比为1:1',
    mini_program_src: '/static/material/mini_program.png',
    mini_program_src_s: '/static/material/mini_program_s.png',
    official_accounts_src: '/static/material/official_accounts.png',
    official_accounts_src_s: '/static/material/official_accounts_s.png',
    type: 2 // 分享的账号类型（小程序 2  公众号 1）
});

const props = withDefaults(
    defineProps<{
        show: boolean;
        id: number; // 素材id
        activity_id: number; // 抽奖裂变
        type: number; // 分享的素材类型 1-文本 2-图片 3-音频 4-视频 5-文件 6-链接文章 7-链接引用 8-抽奖裂变 9-短链
        accountType: number; // 分享的账号类型（小程序 2  公众号 1）
    }>(),
    {
        show: false,
        id: 0,
        activity_id: 0,
        type: 2,
        accountType: 0
    }
);

const form = reactive<any>({
    img_src: '',
    title: '',
    description: '',
    code: ''
});

const emit = defineEmits(['update:show', 'submit']);

const isShowDialog = computed({
    get: () => props.show,
    set: (val: boolean) => emit('update:show', val)
});

const media_content_id = computed(() => {
    return props.activity_id;
});

const closeDialog = () => {
    isShowDialog.value = false;
};

const cancelDialog = () => {
    isShowDialog.value = false;
};

const confirmDialog = () => {
    if (form.img_src && form.title && form.description) {
        isShowDialog.value = false;
    } else {
        ElMessage.error('请完善分享设置');
    }
};

const changeChannel = (type: number) => {
    state.type = type;
};

const inputChange = async () => {
    const id = props.activity_id;
    const title = form.title;
    const desc = form.description;
    const cover = form.img_src;
    const params = { id, title, desc, cover };
    await setLink(params);
};

const imageUploadChange = async (file: any) => {
    const id = props.activity_id;
    const title = form.title;
    const desc = form.description;
    const cover = file ? file.url : '';
    const params = { id, title, desc, cover };
    await setLink(params);
};

const getSettingShareFn = async () => {
    startLoading();
    try {
        const id = props.activity_id;
        const res = await getLink({ id }, true);
        form.img_src = res.cover;
        form.title = res.title;
        form.description = res.desc;
    } catch (error) {
        endLoading();
    }
};

const init = async () => {
    await getSettingShareFn();
};

watch(
    () => props.accountType,
    val => {
        if (val) {
            changeChannel(val);
        }
    },
    { immediate: true }
);

onMounted(async () => {
    await init();
});

defineExpose({
    changeChannel
});
</script>

<style lang="scss" scoped>
.content {
    padding: 6px 12px;

    .module {
        .head {
            display: flex;
            align-items: center;
            margin-bottom: 18px;

            .title {
                padding-left: 6px;
                border-left: 4px solid #ff6b00;
                font-size: 14px;
                color: #333;
                font-weight: 500;
            }
        }

        .share-title {
            margin-bottom: 0;
        }

        .details {
        }

        .tips {
            font-size: 12px;
            color: #888;
            padding-left: 12px;
        }

        .details_setting {
            display: flex;
            align-items: center;
            padding: 6px 12px;

            .input_text {
                height: 100px;
                flex: 1;
                display: flex;
                flex-direction: column;
                justify-content: space-between;

                .input_item {
                    display: flex;
                    align-items: flex-start;

                    &:first-of-type {
                        margin-bottom: 12px;
                    }

                    .input_title {
                        white-space: nowrap;
                        margin-left: 20px;
                        margin-right: 12px;
                        line-height: 2;
                    }
                }
            }
        }
    }

    .setting {
        margin-bottom: 32px;
    }

    .share {
        .share_type {
            padding-top: 0;
            margin-left: 12px;
            margin-bottom: 12px;
            display: flex;
            align-items: center;

            .account {
                display: flex;
                align-items: center;
                margin-right: 32px;
                padding-bottom: 6px;
                border-bottom: 2px solid #fff;

                .el-image {
                    width: 18px;
                    height: 18px;
                    margin-right: 4px;
                }

                span {
                    font-size: 14px;
                    color: #555;
                    white-space: nowrap;
                }

                .selected {
                    color: #ff6b00;
                    font-weight: 800;
                }

                &:hover {
                    cursor: pointer;

                    span {
                        color: #ff6b00;
                    }
                }
            }

            .select {
                border-bottom-color: #ff6b00;
            }

            .mini_program {
            }

            .official_accounts {
            }
        }
    }
}
</style>
